<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('修改资讯')"/>
    <th:block th:include="include :: bootstrap-fileinput-css"/>
    <th:block th:include="include :: summernote-css"/>

    <style type="text/css">

        .imgDiv {
            width: 550px;
            height: 400px;
            position: relative;
            display:block
        }

        .imgDiv .video {
            position:relative;
        }

        .imgDiv .deleteImg {
            position: absolute;
            top: -20px;
            right: -25px;
            width : 40px;
            height :40px;
        }

    </style>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-news-edit" th:object="${news}">
        <input name="newId" th:field="*{newId}" type="hidden">
        <div class="form-group">
            <label class="col-sm-3 control-label">学习标题：</label>
            <div class="col-sm-8">
                <input name="newTitle" th:field="*{newTitle}" class="form-control" type="text" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">学习分类：</label>
            <div class="col-sm-8">
                <select name="newTypeId" class="form-control m-b" th:field="*{newTypeId}">
                    <option th:each="type:${typeList}" th:text="${type.typeName}" th:value="${type.typeId}"/>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">归属部门：</label>
            <div class="col-sm-8">
                <input name="deptId" class="form-control" type="hidden" id="treeId" required>
                <div class="input-group">
                    <input name="deptName" onclick="selectDeptTree()" id="treeName" type="text" th:field="*{deptName}"
                           placeholder="请选择归属部门" class="form-control" required>
                    <span class="input-group-addon"><i class="fa fa-search"></i></span>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">上传时间：</label>
            <div class="col-sm-8 ">
                <div class="input-group date">
                    <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    <input id="createTime" name="createTime" class="form-control" placeholder="请选择时间" type="text" th:value="${#dates.format(news.createTime, 'yyyy-MM-dd HH:mm:ss')}" required>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">学习内容：</label>
            <div class="col-sm-8">
                <input id="newContent" name="newContent" th:field="*{newContent}" class="form-control" type="hidden">
                <div id="editor" class="summernote"></div>
            </div>
        </div>
        <!--    <div class="form-group">
            <label class="col-sm-3 control-label">上传文件：</label>
            <div class="col-sm-8">
            <input name="newFile" th:field="*{newFile}" class="form-control" type="hidden">
            <div class="row">
            <input id="files" name="files" th:value="${files}" class="form-control"  type="hidden">
            <div  class="col-sm-12" th:if="${files!=null && #lists.size(files)>0}">
            <th:block th:each="name:${files}">
            <div class="file-box">
            <div class="grid">
            <div  class="imgholder">
            <img name="newFile" data-magnify="gallery" th:data-src="${name}" th:src="${name}"   style='width:150px;height:150px;' ></img>
            </div>

            </div>
            </div>
            </th:block>
            </div>
            <div class="col-sm-12" th:if="${files==null || #lists.size(files)<1}">
            <span>暂无文件！</span>
            </div>
            </div>
            </div>
            </div>-->
        <div class="form-group">
            <label class="col-sm-3 control-label">上传视频：</label>
            <div class="col-sm-8">
                <input id="newVideo" name="newVideo" th:field="*{newVideo}" class="form-control" type="hidden">
                <div class="file-loading">
                    <input id="fileinput-video" type="file" name="file" data-min-file-count="1"
                           accept="video/mp4" multiple>
                </div>
            </div>
        </div>
        <div class="row" style="margin-top: 40px;">
            <div class="form-group">
                <label class="col-sm-3 control-label"></label>
                <div class="col-sm-8">
                    <div  id="showPrew" style="padding:5px;"></div>
                </div>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: summernote-js"/>
<th:block th:include="include :: bootstrap-fileinput-js"/>
<script type="text/javascript">
    var prefix = ctx + "module/newsLearn";
    $("#form-news-edit").validate({
        focusCleanup: true
    });

    layui.use('laydate', function() {
        var laydate = layui.laydate;
        laydate.render({
            elem: '#createTime',
            type: 'datetime',
            trigger: 'click'
        });
    });

    $(function () {
        $('.summernote').summernote({
            placeholder: '请输入内容',
            height: 300,
            lang: 'zh-CN',
            followingToolbar: false,
            callbacks: {
                onImageUpload: function (files) {
                    sendFile(files[0], this);
                }
            }
        });
        var content = $("#newContent").val();
        $('#editor').summernote('code', content);
    });

    // 上传文件
    function sendFile(file, obj) {
        var data = new FormData();
        data.append("file", file);
        $.ajax({
            type: "POST",
            url: ctx + "common/upload",
            data: data,
            cache: false,
            contentType: false,
            processData: false,
            dataType: 'json',
            success: function (result) {
                if (result.code == web_status.SUCCESS) {
                    $(obj).summernote('editor.insertImage', result.url, result.fileName);
                } else {
                    $.modal.alertError(result.msg);
                }
            },
            error: function (error) {
                $.modal.alertWarning("图片上传失败。");
            }
        });
    }


    /*-新增-选择部门树*/
    function selectDeptTree() {
        var treeId = $("#treeId").val();
        var deptId = $.common.isEmpty(treeId) ? "100" : $("#treeId").val();
        var url = ctx + "system/dept/selectDeptTree/" + deptId;
        var options = {
            title: '选择部门',
            width: "380",
            url: url,
            callBack: doSubmit
        };
        $.modal.openOptions(options);
    }

    function doSubmit(index, layero) {
        var tree = layero.find("iframe")[0].contentWindow.$._tree;
        if ($.tree.notAllowParents(tree)) {
            var body = layer.getChildFrame('body', index);
            $("#treeId").val(body.find('#treeId').val());
            $("#treeName").val(body.find('#treeName').val());
            layer.close(index);
        }
    }

    var videoArr= new Array();

    var videos = $("#newVideo").val();
    if(null!=videos && videos.length>0){
        videoArr = videos.split(",");
        for(var i=0;i<videoArr.length;i++){
            $('#showPrew').append("<div class='imgDiv' id='"+i+"'><img class='deleteImg' src='../../../img/delete.png'  th:src='@{/img/delete.png}' onclick='removeImg("+i+")'/><video src='"+videoArr[i]+"' class='video' autoplay></div>");
        }
    }

    //上传视频
    $(document).ready(function () {
        var uploadUrl = ctx + "common/uploadImage_QiNiu";
            $("#fileinput-video").fileinput({
                dropZoneTitle : "请上传视频！",
                'theme': 'explorer-fas',
                'uploadUrl': uploadUrl,
                overwriteInitial: false,
                allowedFileExtensions: ["mp4"],
                initialPreview: []
            });

        // 上传成功回调
        $("#fileinput-video").on("fileuploaded", function (event, data, previewId, index) {
            console.log(data);
            var videoStr = data.response.data.src;
            videoArr.push(videoStr);
            var video = videoArr.join(",");
            console.log("***"+video);
            $("#newVideo").val(video);

        });
    });

    function removeImg(id) {
        $.modal.confirm("确认要删除选中的图片吗?", function() {
            $("#"+id).remove();
            videoArr.splice(id,1); //删除图片数组中的图片
            var video = videoArr.join(",");
            console.log("Delete后***"+video);
            $("#newVideo").val(video);
        });

    }

    //鼠标划过事件
    $(".imgDiv").mouseover(function () {
        //显示删除叉
        $(this).find(".deleteImg").css("display", "block");
    });
    $(".imgDiv").mouseout(function () {
        //隐藏删除叉
        $(this).find(".deleteImg").css("display", "none");
    });


    function submitHandler() {
        if ($.validate.form()) {
            var sHTML = $('.summernote').summernote('code');
            $("#newContent").val(sHTML);
            $.operate.save(prefix + "/edit", $('#form-news-edit').serialize());
        }
    }
</script>
</body>
</html>